package emailsender;
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**EMailSender provide a method to send the mail to all the recipients.
 * @author Alberto Magni*/
public class EMailSender 
{
    private String table=null;
    private Transport transport=null;
    private String[] recipients=null;
    private MimeMessage message=null;
    private float rate;
    private float limit;
//------------------------------------------------------------------------------
    /**Set the recipients of the mail.
     * @param recipients the list of the recipients of the mail*/
    public void setRecipients(String [] recipients)
    {
        this.recipients=recipients;
    }
//------------------------------------------------------------------------------
    /**Set the mail message.
     * @param message the MIME message that has to be sent*/
    public void setMessage(MimeMessage message)
    {
        this.message=message;
    }
//------------------------------------------------------------------------------
    /**Set the table of the warnings in the mail message.
     * @param table the list of the warnings found by the spam analizer*/
    public void setTable(String table)
    {
        this.table=table;
    }
//------------------------------------------------------------------------------
    /**Set the spam rate of the mail.
     * @param rate the level of spam of the mail*/
    public void setRate(float rate)
    {
        this.rate=rate;
    }
//------------------------------------------------------------------------------
    /**Set the limit over which the mail is considered spam.
     * @param limit the rate above which a mail is marked as spam*/
    public void setLimit(float limit)
    {
        this.limit=limit;
    }
//------------------------------------------------------------------------------
    /**Set the enviroment variable of the connection.
     * @param transport the enviroment variable in which the mail is sent*/
    public void setTransport(Transport transport)
    {
        this.transport=transport;
    }
//------------------------------------------------------------------------------
    /**Send the email to all the recipients*/
    public void sendMail() throws Exception,SendFailedException
    {       
        for(int index=0;index<recipients.length;index++)
        {
            Address to_address=new InternetAddress(recipients[index].trim());
            message.setRecipient(Message.RecipientType.TO,to_address);
            transport.sendMessage(message, message.getAllRecipients());
        }
        transport.close();
    }
}